package com.woniuxy.car.commodity.service.core.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.woniuxy.car.commodity.service.core.constant.ParamLabel;
import com.woniuxy.car.commodity.service.core.dto.CommodityFormatAndParamDto;
import com.woniuxy.car.commodity.service.core.dto.CommodityParamDto;
import com.woniuxy.car.commodity.service.core.entity.CommodityParam;
import com.woniuxy.car.commodity.service.core.service.ICommodityParamService;
import com.woniuxy.common.api.CommonResult;
import com.woniuxy.common.api.enums.ResultCode;
import com.woniuxy.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * 商品参数表 前端控制器
 * </p>
 *
 * @author awz
 * @since 2022-03-01
 */
@Api(tags = "商品参数表")
@RestController
@RequestMapping("/commodity/commodity-param")
public class CommodityParamController {


    @Autowired(required = false)
    ICommodityParamService commodityParamService;

    @ApiOperation("根据商品id查询参数")
    @GetMapping("/{commodityId}")
    public CommonResult<List<CommodityFormatAndParamDto>> getOneById(@PathVariable("commodityId") Integer commodityId){

        try {
            CommodityParam commodityParam = commodityParamService.findOne(commodityId);
            //传给前端DTO实体类List
            List<CommodityFormatAndParamDto> formatAndParamDtos = new ArrayList<>();

            if (commodityParam!=null) {
                //得到json数组
                JSONArray jsonArray = JSONUtil.parseArray(commodityParam.getParamData());

                for (Object object : jsonArray) {
                    //传给前端DTO实体类
                    CommodityFormatAndParamDto formatAndParamDto = new CommodityFormatAndParamDto();
                    //封装规格key-value实体类
                    List<CommodityParamDto> paramDtos = new ArrayList<>();

                    JSONObject jsonObject = JSONUtil.parseObj(object);

                    //得到规格参数或者商品参数
                    String str = (String)jsonObject.get("group");

                    //根据查到的标签设置DTO的label名
                    if(str.equals(ParamLabel.PARAM_GG)){
                        formatAndParamDto.setLabel(ParamLabel.PARAM_GG);
                    }else if(str.equals(ParamLabel.PARAM_SP)){
                        formatAndParamDto.setLabel(ParamLabel.PARAM_SP);
                    }else{
                        formatAndParamDto.setLabel(null);
                    }
                    //解析
                    List<HashMap<String,String>> params = (List<HashMap<String,String>>) jsonObject.get("params");

                    for (Object obj : params) {
                        System.out.println(obj);
                        Map<String,String> map = new JSONObject(obj).toBean(Map.class);
                        CommodityParamDto paramDto = new CommodityParamDto();
                        paramDto.setKey(map.get("key"));
                        paramDto.setValue(map.get("value"));
                        paramDtos.add(paramDto);
                    }
                    formatAndParamDto.setLabelList(paramDtos);
                    formatAndParamDtos.add(formatAndParamDto);
                }
            }
            return CommonResult.success(formatAndParamDtos);
        }catch (BusinessException e){
            return CommonResult.failure(ResultCode.FAILED.getCode(),e.getMessage());
        }

    }
}
